/**
 * This implements Writable instead of extending ArrayWritable for efficiency
 * reasons.
 * 
 * 
 * TODO: use VInt storage
 * 
 * @author hc.busy
 * 
 */

package org.yagnus.yadoop
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.ArrayWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableFactories;

class IntArrayWritable extends Writable {
    var array : Array[Int] = null;

    def this(in : Array[Int]) = {
        this();
        set(in)
    }

    def set(values : Array[Int]) = {
        this.array = values;
    }

    def get : Array[Int] = array;

    override def readFields(in : DataInput) {
        array = new Array[Int](in.readInt); // construct values
        for (i ← 0 until array.length)
            array(i) = in.readInt
    }

    override def write(out : DataOutput) {
        if (array == null) {
            out.writeInt(0);
        } else {
            out.writeInt(array.length);
            for (l ← array) {
                out.writeInt(l);
            }
        }
    }

}
